<template>
  <view class="container">
    <!-- 顶部用户信息 -->
    <view class="header">
      <view class="couple-info">
        <view class="avatar-container">
          <image class="avatar" :src="userAvatar" mode="aspectFill" />
          <image class="avatar partner" :src="partnerAvatar" mode="aspectFill" />
        </view>
        <view class="couple-details">
          <text class="names">{{ coupleNames }}</text>
          <text class="relationship-days">在一起 {{ relationshipDays }} 天</text>
        </view>
      </view>
      <view class="points-display" @tap="goToPoints">
        <text class="points">{{ totalPoints }}</text>
        <u-icon name="heart-fill" color="#ff6b81" size="16" />
        <text class="points-label">爱心积分</text>
      </view>
    </view>

    <!-- 轮播图 -->
    <view class="banner-section">
      <u-swiper
        :list="bannerList"
        indicator
        indicatorMode="line"
        circular
        height="300rpx"
        radius="12"
        @click="handleBannerClick"
      />
    </view>

    <!-- 快捷操作 -->
    <view class="quick-actions">
      <view class="action-item" @tap="goToMenu">
        <view class="action-icon">
          <u-icon name="order" color="#ff6b81" size="48" />
        </view>
        <text class="action-name">点餐</text>
      </view>
      <view class="action-item" @tap="goToPoints">
        <view class="action-icon">
          <u-icon name="integral" color="#ff6b81" size="48" />
        </view>
        <text class="action-name">积分</text>
      </view>
      <view class="action-item" @tap="goToEvent">
        <view class="action-icon">
          <u-icon name="calendar" color="#ff6b81" size="48" />
        </view>
        <text class="action-name">事件</text>
      </view>
      <view class="action-item" @tap="goToNotifications">
        <view class="action-icon">
          <u-icon name="bell" color="#ff6b81" size="48" />
        </view>
        <text class="action-name">通知</text>
      </view>
    </view>

    <!-- 推荐菜品 -->
    <view class="section">
      <view class="section-header">
        <text class="section-title">今日推荐</text>
        <text class="section-more" @tap="goToMenu">更多 ></text>
      </view>
      <view class="dish-list">
        <dish-card
          v-for="dish in recommendDishes"
          :key="dish.id"
          :dish="dish"
          @click="goToDishDetail"
        />
      </view>
    </view>

    <!-- 积分任务 -->
    <view class="section">
      <view class="section-header">
        <text class="section-title">积分任务</text>
        <text class="section-more" @tap="goToTasks">查看全部 ></text>
      </view>
      <view class="tasks-list">
        <view 
          v-for="task in dailyTasks"
          :key="task.id"
          class="task-card"
          @tap="completeTask(task)"
        >
          <view class="task-icon">
            <u-icon :name="task.icon" color="#ff6b81" size="32" />
          </view>
          <view class="task-info">
            <text class="task-name">{{ task.name }}</text>
            <text class="task-points">+{{ task.points }} 积分</text>
          </view>
          <button 
            class="task-btn" 
            :class="{ completed: task.completed }"
            :disabled="task.completed"
          >
            {{ task.completed ? '已完成' : '领取' }}
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useDishStore } from '@/store/modules/dish'
import DishCard from '@/components/dish-card/index.vue'

const userStore = useUserStore()
const dishStore = useDishStore()

// 计算属性
const userAvatar = computed(() => userStore.userAvatar)
const partnerAvatar = computed(() => userStore.partnerAvatar)
const coupleNames = computed(() => userStore.coupleNames)
const relationshipDays = computed(() => userStore.relationshipDays)
const totalPoints = computed(() => userStore.totalPoints)
const recommendDishes = computed(() => dishStore.recommendDishes)

// 数据
const bannerList = ref([
  {
    url: '/static/images/banner1.png',
    title: '今日特餐：浪漫烛光晚餐'
  },
  {
    url: '/static/images/banner2.png',
    title: '甜蜜双人套餐限时优惠'
  }
])

const dailyTasks = ref([
  {
    id: 1,
    name: '早安吻',
    points: 10,
    icon: 'heart',
    completed: false
  },
  {
    id: 2,
    name: '洗碗',
    points: 20,
    icon: 'setting',
    completed: false
  },
  {
    id: 3,
    name: '一起做饭',
    points: 15,
    icon: 'home',
    completed: true
  }
])

// 方法
const goToMenu = () => {
  uni.switchTab({ url: '/pages/menu/index' })
}

const goToPoints = () => {
  uni.navigateTo({ url: '/pages/points/index' })
}

const goToEvent = () => {
  uni.navigateTo({ url: '/pages/event/index' })
}

const goToNotifications = () => {
  uni.showToast({
    title: '通知功能开发中',
    icon: 'none'
  })
}

const goToDishDetail = (dish) => {
  uni.navigateTo({
    url: `/pages/menu/detail?id=${dish.id}`
  })
}

const goToTasks = () => {
  uni.navigateTo({ url: '/pages/points/tasks' })
}

const completeTask = (task) => {
  if (!task.completed) {
    task.completed = true
    userStore.setPoints(userStore.totalPoints + task.points)
    uni.showToast({
      title: `获得 ${task.points} 积分`,
      icon: 'success'
    })
  }
}

const handleBannerClick = (index) => {
  const banner = bannerList.value[index]
  uni.showToast({
    title: banner.title,
    icon: 'none'
  })
}

onMounted(() => {
  dishStore.getDishes()
})
</script>

<style lang="scss" scoped>
.container {
  padding: 20rpx;
  background: #f5f5f5;
  min-height: 1334rpx;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  padding: 30rpx;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  
  .couple-info {
    display: flex;
    align-items: center;
    
    .avatar-container {
      position: relative;
      width: 100rpx;
      height: 80rpx;
      margin-right: 20rpx;
      
      .avatar {
        width: 70rpx;
        height: 70rpx;
        border-radius: 50%;
        border: 3rpx solid #ff6b81;
        position: absolute;
        left: 0;
        top: 5rpx;
        
        &.partner {
          left: 30rpx;
          top: 5rpx;
          z-index: -1;
}
      }
    }
    
    .couple-details {
      .names {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
        display: block;
        margin-bottom: 8rpx;
}
      
      .relationship-days {
        font-size: 24rpx;
        color: #666;
        
        &::before {
          content: '❤️ ';
}
      }
    }
  }
  
  .points-display {
    text-align: center;
    
    .points {
      font-size: 36rpx;
      font-weight: bold;
      color: #ff6b81;
      display: block;
}
    
    .points-label {
      font-size: 24rpx;
      color: #666;
}
  }
}

.banner-section {
  margin-bottom: 30rpx;
}

.quick-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
   margin-right: 20rpx; &:last-child { margin-right: 0; }
  margin-bottom: 30rpx;
  
  .action-item {
    background: #fff;
    padding: 30rpx 20rpx;
    border-radius: 20rpx;
    text-align: center;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    
    .action-icon {
      margin-bottom: 10rpx;
}
    
    .action-name {
      font-size: 24rpx;
      color: #666;
}
  }
}

.section {
  margin-bottom: 30rpx;
  
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    
    .section-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
}
    
    .section-more {
      font-size: 24rpx;
      color: #999;
}
  }
  
  .dish-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
     margin-right: 20rpx; &:last-child { margin-right: 0; }
  }
  
  .tasks-list {
    .task-card {
      display: flex;
      align-items: center;
      background: #fff;
      padding: 30rpx;
      border-radius: 20rpx;
      margin-bottom: 20rpx;
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
      
      .task-icon {
        margin-right: 20rpx;
}
      
      .task-info {
        flex: 1;
        
        .task-name {
          font-size: 28rpx;
          font-weight: bold;
          color: #333;
          display: block;
          margin-bottom: 4rpx;
}
        
        .task-points {
          font-size: 24rpx;
          color: #ff6b81;
}
      }
      
      .task-btn {
        background: #ff6b81;
        color: #fff;
        font-size: 24rpx;
        padding: 10rpx 20rpx;
        border-radius: 20rpx;
        
        &.completed {
          background: #ccc;
}
      }
    }
  }
}
</style>
